<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    
<?php 
$layout = explode('&&&', file_get_contents('layout.html'));
echo $layout[0];

session_start();
if (!(isset($_SESSION['username'])))
	header("location:login.php");
	
$id=$_GET['id'];
?>

<html>
<head>
 <script language="JavaScript" src="calendar3.js"></script>
</head>
<style type="text/css">
<!--
.style1 {color: #FF0000}
-->
</style>
<body>

<!-- Begin Main Column -->

<div id="mainContent">
	
  <h2>Find <?php if($id==1){echo " & Edit ";}elseif($id==2){echo " & Delete ";}?>Workshop</h2>
      
      <p></p>Find Workshop
  <form id="form1" method="post" action="">
    <table width="200" border="0">
            <tr>
              <td>Date:</td>
              <td><label>
                <input name="date" type="text" id="date" size="10" maxlength="10" />
                <a href="javascript:cal.popup();"><img src="img/cal.gif" /></a>
              </label></td>
            </tr>
          </table>
          <p>
            <label>
            <input type="submit" name="workshop_search" id="workshop_search" value="Search" class="button"/>
            </label>
            </p>

  </form>
  <script>var cal = new calendar3(document.forms['form1'].elements['date']);</script>
  <p>&nbsp;</p>

<!-- Begin Side Column -->
<!-- Begin Footer -->

<?php
	 	if($_POST) {
			ob_start();
			$host="localhost"; // Host name
			$username="root"; // Mysql username
			$password=""; // Mysql password
			$db_name="rtl"; // Database name
			
			// Connect to server and select databse.
			mysql_connect("$host", "$username", "$password")or die("cannot connect");
			mysql_select_db("$db_name")or die("cannot select DB");
			
			$date=$_POST['date'];	
			$action=$_GET['id'];
			
			// To protect MySQL injection (more detail about MySQL injection)
			$date = stripslashes($date);

			$date = mysql_real_escape_string($date);
			
			if($date != "") {
				$workshop_by_date = mysql_query("SELECT WRKSPID,WRKSPDATE,WRKSPTIME,LOCATION,WRKSPTOPICID FROM WORKSHOP WHERE WRKSPDATE='$date'");
    			echo "<table border='1'>";
  				echo "<tr> <th>Date</th> <th>Time</th> <th>Location</th> <th>Topic</th>";
  				//keeps getting the next row until there are no more to get
				while($row = mysql_fetch_array($workshop_by_date))
				{
					$workshop_topic = $row['WRKSPTOPICID'];
					$workshop_by_topic=mysql_query("SELECT WRKSPTOPIC FROM WORKSHOPTOPICS WHERE WRKSPTOPICID='$workshop_topic'");
					$workshop_topic = mysql_result($workshop_by_topic,0);
					$workshop_id= $row['WRKSPID'];
					// Print out the contents of each row into a table
					echo "<tr><td>";
					if($action==1) {
						echo "<a href=\"EditWorkshop.php?id=$workshop_id\">".$row['WRKSPDATE']."</a>";
					}
					else if($action==2) {
						echo "<a href=\"DeleteEvent.php?id=$workshop_id&action=1\">".$row['WRKSPDATE']."</a>";
					}
					echo "</td><td>";
					echo getTime($row['WRKSPTIME']);
					echo "</td><td>";
					echo $row['LOCATION'];
					echo "</td><td>";
					echo $workshop_topic;
				}
				echo "</table>";
			}
		}
			

	?>
    </div>
    
 <?php
 function getTime($timestring)
{

	// split time into hour, minute, and second integers
	$time_array = explode(":", $timestring);
	$time_hour = $time_array[0];
	$time_minute = $time_array[1];
	
	$am_pm = "a.m.";
	// fix am/pm
	// if time is PM, add 12 to the hours integer
	if ($time_hour > 12)
	{
		$time_hour = $time_hour-12;
		$am_pm = "p.m.";
	}
	$time= $time_hour.":".$time_minute." ".$am_pm;
	return $time;
}
?>

<?php
echo $layout[1];
?>
</body>
</html>
